package com.sanji.sjmallmodule.order;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallmodule.goods.GoodsDetailsActivity;
import com.sanji.sjmallutils.Tools;
import com.sanji.sjmallwidget.MyListView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单列表
 * Created by zsf on 17-1-14.
 */

public class MyOrderListAdapter extends BaseAdapter implements View.OnClickListener, AdapterView.OnItemClickListener {

    private Context mCtx;
    private List<MyOrderBean> mList;
    private BuyAgainOrderListener buyAgainOrderListener;
    private CancelOrderListener cancelOrderListener;
    private PayOrderListener payOrderListener;
    MyOrderBean order;

    public MyOrderListAdapter(Context context, List<MyOrderBean> list) {
        this.mCtx = context;
        this.mList = list;
    }

    public void setPayOrderListener(PayOrderListener payOrderListener) {
        this.payOrderListener = payOrderListener;
    }

    public void setCancelOrderListener(CancelOrderListener cancelOrderListener) {
        this.cancelOrderListener = cancelOrderListener;
    }

    public void setBuyAgainOrderListener(BuyAgainOrderListener buyAgainOrderListener) {
        this.buyAgainOrderListener = buyAgainOrderListener;
    }

    public void notifyData(List<MyOrderBean> mData) {
        this.mList = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public MyOrderBean getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_list_my_order, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.mOrderListItemToPayBtn.setOnClickListener(this);
        holder.mOrderListItemToPayBtn.setTag(i);
        holder.mOrderListItemDelOrderBtn.setOnClickListener(this);
        holder.mOrderListItemDelOrderBtn.setTag(i);

        holder.mOrderListItemAgainBuyBtn.setOnClickListener(this);
        holder.mOrderListItemAgainBuyBtn.setTag(i);

        holder.mMyListView.setEnabled(false);

        order = getItem(i);
        holder.mOrderListItemOrderno.setText(mCtx.getResources().getString(R.string.order_receive_no) + order.getOrderNo());
        holder.mOrderListItemDate.setText(order.getBuyDate());
        holder.mOrderListItemRever.setText(order.getReceiverName());
        holder.mOrderListItemCoast.setText(mCtx.getResources().getString(R.string.price_title) + order.getAmount());
        holder.mOrderListItemFat.setText(String.format(mCtx.getResources().getString(R.string.order_fare),order.getFare()));
        int payStatus = order.getPayStatus();//0-未付款1-已付款
        int shipStatus = order.getShipStatus();//0-未发货1-已出库2-已发货3-已收货（已签收）4-买家拒签5-待退货
        int payType = order.getPaidType();//0-网上支付1-货到付款2-pos支付
        int orderStatus = order.getOrderStatus();//0正常，1 未审核，2 已审核，3取消，4 无效

        if (orderStatus == 3 || orderStatus == 4) {
            holder.orderCloseTv.setVisibility(View.VISIBLE);
            holder.orderCloseTv.setText(mCtx.getResources().getString(R.string.order_close));
            holder.mOrderListItemOrderTypeTv.setVisibility(View.GONE);
            holder.mOrderListItemOrderOutstockTv.setVisibility(View.GONE);
            holder.mOrderListItemToPayBtn.setVisibility(View.GONE);
            holder.mOrderListItemDelOrderBtn.setVisibility(View.GONE);
        } else if (payStatus == 0) {
            holder.mOrderListItemOrderTypeTv.setText(mCtx.getResources().getString(R.string.order_unpay));
            holder.mOrderListItemOrderTypeTv.setVisibility(View.VISIBLE);
            holder.mOrderListItemToPayBtn.setVisibility(View.VISIBLE);
            holder.mOrderListItemDelOrderBtn.setVisibility(View.VISIBLE);

            holder.orderCloseTv.setVisibility(View.GONE);
            holder.mOrderListItemOrderOutstockTv.setVisibility(View.GONE);
            if (payType == 1){
                holder.mOrderListItemToPayBtn.setVisibility(View.GONE);
                holder.mOrderListItemOrderOutstockTv.setVisibility(View.VISIBLE);
                holder.mOrderListItemOrderOutstockTv.setText(mCtx.getResources().getString(R.string.order_pay_case));
            }
            if(shipStatus!=0){
                holder.mOrderListItemDelOrderBtn.setVisibility(View.GONE);
            }
        } else if (shipStatus == 0) {
            holder.mOrderListItemOrderTypeTv.setText(mCtx.getResources().getString(R.string.order_un_delivery));
            holder.mOrderListItemOrderTypeTv.setVisibility(View.VISIBLE);
            holder.mOrderListItemDelOrderBtn.setVisibility(View.VISIBLE);

            holder.orderCloseTv.setVisibility(View.GONE);
            holder.mOrderListItemOrderOutstockTv.setVisibility(View.GONE);
            holder.mOrderListItemOrderTypeTv.setVisibility(View.GONE);

            if(payStatus == 1){
                holder.mOrderListItemDelOrderBtn.setVisibility(View.GONE);
                holder.mOrderListItemOrderOutstockTv.setVisibility(View.VISIBLE);
                holder.mOrderListItemOrderOutstockTv.setText(mCtx.getResources().getString(R.string.order_payed_online));
            }

        } else {
            holder.orderCloseTv.setVisibility(View.GONE);
            holder.mOrderListItemOrderTypeTv.setVisibility(View.GONE);
            holder.mOrderListItemToPayBtn.setVisibility(View.GONE);
            holder.mOrderListItemDelOrderBtn.setVisibility(View.GONE);

            holder.mOrderListItemOrderOutstockTv.setVisibility(View.VISIBLE);

            StringBuffer shipBuff = new StringBuffer();


            //物流提示
            switch (shipStatus) {
                case 1:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_has_delivery));
                    break;
                case 2:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_delivered));
                    break;
                case 3:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_signed));
                    switch (payType) {
                        case 0:
                            shipBuff.append(mCtx.getResources().getString(R.string.order_ship_online_pay));
                            break;
                        case 1:
                            shipBuff.append(mCtx.getResources().getString(R.string.order_cash_on_delivery));
                            break;
                        case 2:
                            shipBuff.append(mCtx.getResources().getString(R.string.order_pos_on_delivery));
                            break;
                    }
                    break;
                case 4:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_goods_returning));
                    break;
                case 5:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_goods_returned));
                    break;
                case 6:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_goods_refuse_return));
                    break;
                case 7:
                    shipBuff.append(mCtx.getResources().getString(R.string.order_buyer_refused));
                    break;

            }
            holder.mOrderListItemOrderOutstockTv.setText(shipBuff.toString());
        }
        List<ShopcarChildBean> goodsList = order.getGoodsList();

        MyOrderItemAdapter mItemAdapter = new MyOrderItemAdapter(mCtx, goodsList);
        holder.mMyListView.setAdapter(mItemAdapter);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_list_item_to_pay_btn:
                if (payOrderListener != null) {
                    int position = (int) view.getTag();
                    payOrderListener.payOrder(mList.get(position));
                }
                break;
            case R.id.order_list_item_del_order_btn:
                if (null != cancelOrderListener) {
                    int position = (int) view.getTag();
                    cancelOrderListener.cancelOrder(mList.get(position));
                }
                break;
            case R.id.order_list_item_again_buy_btn:
                if (null != buyAgainOrderListener) {
                    int position = (int) view.getTag();
                    buyAgainOrderListener.buyAgainOrder(mList.get(position));
                }
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        int pos = (int) view.getTag();
        MyOrderBean order = getItem(pos);

        Intent intent = new Intent(mCtx, MyOrderDestActivity.class);
        intent.putExtra("order", order);
        mCtx.startActivity(intent);
    }

    static class ViewHolder {
        @BindView(R.id.order_list_item_orderno)
        TextView mOrderListItemOrderno;
        @BindView(R.id.order_list_item_date)
        TextView mOrderListItemDate;
        @BindView(R.id.order_list_item_rever)
        TextView mOrderListItemRever;
        @BindView(R.id.order_list_item_coast)
        TextView mOrderListItemCoast;
        @BindView(R.id.order_list_item_fat)
        TextView mOrderListItemFat;
        @BindView(R.id.order_list_item_order_type_tv)
        TextView mOrderListItemOrderTypeTv;
        @BindView(R.id.order_list_item_order_outstock_tv)
        TextView mOrderListItemOrderOutstockTv;
        @BindView(R.id.order_list_item_to_pay_btn)
        TextView mOrderListItemToPayBtn;
        @BindView(R.id.order_list_item_del_order_btn)
        TextView mOrderListItemDelOrderBtn;
        @BindView(R.id.order_list_item_again_buy_btn)
        TextView mOrderListItemAgainBuyBtn;
        @BindView(R.id.order_list_item_listview)
        MyListView mMyListView;
        @BindView(R.id.order_close_tv)
        TextView orderCloseTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 取消订单
     */
    public interface CancelOrderListener {
        public void cancelOrder(MyOrderBean orderBean);
    }

    /**
     * 立即支付
     */
    public interface PayOrderListener {
        public void payOrder(MyOrderBean orderBean);
    }

    /**
     * 再次购买
     */
    public interface BuyAgainOrderListener {
        public void buyAgainOrder(MyOrderBean orderBean);
    }
}
